package org.meow.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.meow.dto.ArticleCateDTO;
import org.meow.entity.ArticleGroup;
import org.meow.mapper.ArticleCateMapper;
import org.meow.service.ArticleCateService;
import org.meow.utils.MeowStr;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * &#064;Date: 24/04/26-13:11
 * &#064;Description:
 */
@Service
public class ArticleGroupServiceImpl implements ArticleCateService {

  @Autowired
  private ArticleCateMapper articleGroupMapper;

  @Override
  public List<ArticleGroup> getArticleGroup() {
    return articleGroupMapper.selectList(null);
  }

  @Override
  public void save(ArticleCateDTO articleCateDTO) {
    ArticleGroup articleGroup = new ArticleGroup();
    BeanUtils.copyProperties(articleCateDTO, articleGroup);
    articleGroupMapper.insert(articleGroup);
  }

  @Override
  public boolean update(Integer id, ArticleCateDTO cateDTO) {
    LambdaUpdateWrapper<ArticleGroup> updateWrapper = new LambdaUpdateWrapper<>();
    updateWrapper
        .eq(ArticleGroup::getId, id)
        .set(MeowStr.isStrNotBlank(cateDTO.getName()), ArticleGroup::getName, cateDTO.getName())
        .set(MeowStr.isStrNotBlank(cateDTO.getBrief()), ArticleGroup::getBrief, cateDTO.getBrief())
        .set(MeowStr.isStrNotBlank(cateDTO.getLogo()), ArticleGroup::getLogo, cateDTO.getLogo());
    return articleGroupMapper.update(null, updateWrapper) != 0;
  }

  @Override
  public boolean delete(Integer id) {
    return articleGroupMapper.deleteById(id) != 0;
  }

  @Override
  public ArticleGroup getArticleGroupById(Integer id) {
    return articleGroupMapper.selectById(id);
  }
}
